<?php

/**
 * 探针信息
 * @see http://cn.v-checker.com/tz.php
 */
class Probe
{
    public function getProbeData()
    {
        $data = [];

        $data['server_info'] = [
            ['text' => '操作系统', 'minValue' => '不限制', 'recommendValue' => 'Linux', 'value' => PHP_OS, 'comment' => ''],
            ['text' => 'PHP版本', 'minValue' => '8.0', 'recommendValue' => '8.1', 'value' => PHP_VERSION, 'comment' => '使用路由注解需要最低PHP8.0'],
            ['text' => 'MySQL版本', 'minValue' => '8.0', 'recommendValue' => '8.0', 'value' => '-', 'comment' => '无法获取：未配置数据库连接信息'],
            ['text' => '附件上传', 'minValue' => '2M', 'recommendValue' => '2M', 'value' => ini_get('upload_max_filesize'), 'comment' => ''],
            ['text' => 'GD库版本', 'minValue' => '2.0', 'recommendValue' => '2.1', 'value' => GD_VERSION, 'comment' => '图像压缩功能需要GD库'],
            ['text' => '磁盘空间', 'minValue' => '100MB', 'recommendValue' => '不限制', 'value' => CommonUtils::formatBytes(@disk_free_space(".")), 'comment' => ''],
        ];
        $data['module_info'] = [
            ['text' => 'CURL', 'status' => function_exists('curl_init'), 'comment' => ''],
            ['text' => 'OpenSSL', 'status' => function_exists('openssl_open'), 'comment' => ''],
            ['text' => 'PDO Mysql', 'status' => extension_loaded('pdo_mysql'), 'comment' => ''],
            ['text' => 'GD', 'status' => extension_loaded('gd'), 'comment' => ''],
            ['text' => 'mbstring', 'status' => extension_loaded('mbstring'), 'comment' => ''],
            ['text' => 'SimpleXML', 'status' => extension_loaded('SimpleXML'), 'comment' => ''],
        ];
        $data['function_info'] = [
            ['text' => 'curl_init()', 'status' => function_exists('curl_init'), 'comment' => ''],
            ['text' => 'chmod()', 'status' => function_exists('chmod'), 'comment' => ''],
            ['text' => 'mb_substr()', 'status' => function_exists('mb_substr'), 'comment' => ''],
            ['text' => 'simplexml_load_string()', 'status' => function_exists('simplexml_load_string'), 'comment' => ''],
        ];
        $data['env_info'] = [
            ['text' => '/.env', 'is_file' => true, 'status' => false, 'is_exists' => false, 'comment' => '外部配置文件'],
            ['text' => '/public/uploads/', 'is_file' => false, 'status' => false, 'is_exists' => false, 'comment' => '用户文件上传目录'],
            ['text' => '/runtime', 'is_file' => false, 'status' => false, 'is_exists' => false, 'comment' => '临时缓存目录'],
            ['text' => '/public/install/runtime/', 'is_file' => false, 'status' => false, 'is_exists' => false, 'comment' => '临时安装目录'],
        ];
        $i = 0;
        foreach ($data['env_info'] as $row) {
            $row['path'] = '../../..' . $row['text'];
            $row['status'] = is_writable($row['path']);
            $row['is_exists'] = $row['is_file'] ? is_file($row['path']) : is_dir($row['path']);
            $data['env_info'][$i] = $row;
            $i++;
        }

        return $data;
    }
    // 整数运算能力测试
    function testInt()
    {
        $timeStart = gettimeofday();
        for ($i = 0; $i < 3000000; $i++) {
            $t = 1 + 1;
        }
        $timeEnd = gettimeofday();
        $time = ($timeEnd["usec"] - $timeStart["usec"]) / 1000000 + $timeEnd["sec"] - $timeStart["sec"];
        $time = round($time, 3) . "秒";
        return $time;
    }
    // 浮点运算能力测试
    function testFloat()
    {
        // 得到圆周率值
        $t = pi();
        $timeStart = gettimeofday();
        for ($i = 0; $i < 3000000; $i++) {
            // 开平方
            sqrt($t);
        }
        $timeEnd = gettimeofday();
        $time = ($timeEnd["usec"] - $timeStart["usec"]) / 1000000 + $timeEnd["sec"] - $timeStart["sec"];
        $time = round($time, 3) . "秒";
        return $time;
    }
    // IO能力测试
    function testIO()
    {
        $fp = @fopen($_SERVER['SCRIPT_FILENAME'], "r");
        $timeStart = gettimeofday();
        for ($i = 0; $i < 10000; $i++) {
            @fread($fp, 10240);
            @rewind($fp);
        }
        $timeEnd = gettimeofday();
        @fclose($fp);
        $time = ($timeEnd["usec"] - $timeStart["usec"]) / 1000000 + $timeEnd["sec"] - $timeStart["sec"];
        $time = round($time, 3) . "秒";
        return ($time);
    }
}
